package com.jcnet.bizaia;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.jcnet.bizaia.tools.BBTools;
import com.jcnet.bizaia.tools.FileUtils;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class JCSelectPicPopupWindow extends Activity implements OnClickListener{

	private Button btn_take_photo, btn_pick_photo, btn_cancel;
	private LinearLayout layout;
	private Bitmap bitmap;

	public static final int NONE = 0;
	public static final int PHOTOHRAPH = 1;// 拍照
	public static final int PHOTOZOOM = 2; // 缩放
	public static final int PHOTORESOULT = 3;// 结果

	public static final String IMAGE_UNSPECIFIED = "image/*";

	protected String photoDATA;

	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alert_dialog);
		
	
		
		btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo);
		btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);
		btn_cancel = (Button) this.findViewById(R.id.btn_cancel);

		layout=(LinearLayout)findViewById(R.id.pop_layout);



		layout.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// 点击窗口以外的时候finish
				finish();
			}
		});
		btn_cancel.setOnClickListener(this);
		btn_pick_photo.setOnClickListener(this);
		btn_take_photo.setOnClickListener(this);

	}

	@Override
	public boolean onTouchEvent(MotionEvent event){
		//finish();
		return true;
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_take_photo:   //拍照
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(/*new File("/mnt/sdcard/temp.png"))*/new File(BBTools.getSDPath()+"/", "temp.png")));
			startActivityForResult(intent, PHOTOHRAPH);


			break;
		case R.id.btn_pick_photo:		//本地		
			Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT, null);
			intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					IMAGE_UNSPECIFIED);
			startActivityForResult(intent2, PHOTOZOOM);
			break;
		case R.id.btn_cancel:	
			finish();
			break;
		default:
			break;
		}
		//finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == NONE)
			return;
		// 拍照
		if (requestCode == PHOTOHRAPH) {
			// 设置文件保存路径这里放在跟目录下
			File picture = /*new File("/mnt/sdcard/temp.png")*/new File(BBTools.getSDPath()+"/", "temp.png");
			startPhotoZoom(Uri.fromFile(picture));


		}

		if (data == null)
			return;

		// 读取相册缩放图片
		if (requestCode == PHOTOZOOM) {

			if (data != null)

			startPhotoZoom(data.getData());
		}
		// 处理结果
		if (requestCode == PHOTORESOULT) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				//本地返回的结果     照相的结果直接是在sdka
				Bitmap photo = extras.getParcelable("data");

				photoDATA = Base64.encodeToString(FileUtils.getBitmapByte(photo), Base64.DEFAULT);
				
				FileOutputStream fos = null;
				try {
					fos = new FileOutputStream(new File(BBTools.getSDPath()+"/", "temp.png"));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				//bitmap.compress(CompressFormat.PNG, 100, fos);
				
				//ByteArrayOutputStream stream = new ByteArrayOutputStream();
				photo.compress(Bitmap.CompressFormat.PNG, 100, fos);// (0 - 100)压缩文件 75

				//FileUtils.writeFile(FileUtils.getBitmapByte(photo),/*"/mnt/sdcard/"*/BBTools.getSDPath()+"/", "temp.png");
				//imageView.setImageBitmap(photo);     //返回结果 可以直接甚至图片
				Intent intent = new Intent();
				intent.putExtra("photoData", photoDATA);
				setResult(BBTools.PHOTO_RESULT_CODE,intent);

			}

			finish();
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	public void startPhotoZoom(Uri uri) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		intent.putExtra("aspectX", 1);   //1
		intent.putExtra("aspectY", 1);    //1
		// outputX outputY 是裁剪图片宽高
		intent.putExtra("outputX",256);  //64
		intent.putExtra("outputY", 256);
		intent.putExtra("return-data", true);
		startActivityForResult(intent, PHOTORESOULT);
	}
	


}
